﻿using MySql.Data.MySqlClient;
using RightControl.Common;

namespace RightControl.Repository {
    public class MySqlHelper {
        public static string mysqlconnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["mysqlconn"].ConnectionString;
        public static MySqlConnection connection;
        public static System.Data.IDbConnection GetConnection111() {
            //if (connection==null) {
            //    connection = new MySqlConnection(mysqlconnectionString + string.Format(";pwd={0};", DESEncrypt.Decrypt("CBD053595C0F29350B3D64DB684195F0")));
            //    connection.Open();
            //}
            //else {
            //    if (connection.State != System.Data.ConnectionState.Open) {
            //        connection.Open();
            //    }
            //}

            connection = new MySqlConnection(mysqlconnectionString + string.Format(";pwd={0};", DESEncrypt.Decrypt("CBD053595C0F29350B3D64DB684195F0")));
            connection.Open();

            return connection;
        }


        private static MySqlConnectionPool tempPool_A = null;
        private static readonly object readPoolA = new object();
        public static MySqlConnectionPool getTempPool_A() {
            //双if加锁，有且只创建一个连接池
            if (tempPool_A == null) {
                lock (readPoolA) {
                    if (tempPool_A == null) {
                        tempPool_A = new MySqlConnectionPool(mysqlconnectionString + string.Format(";pwd={0};", DESEncrypt.Decrypt("26C064560F9BF6323490EA2C02246FC3")));
                    }
                }
            }
            return tempPool_A;
        }

        public static MySqlConnection GetConnection() {
            return getTempPool_A().getFreeConnectItem().mySqlConn;
        }
    }

}